import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xbaistack/common/gen.dart'; // 末尾有贴，道友莫慌~

import 'package:xbaistack/l10n/language.dart';
import 'package:xbaistack/provider/theme_provider.dart';
import 'package:xbaistack/utils/helper.dart';
import 'package:xbaistack/widget/group_setting.dart';
import 'package:xbaistack/provider/language_provider.dart';

class SettingThemePage extends StatelessWidget {
  const SettingThemePage({super.key});

  @override
  Widget build(BuildContext context) {
    final items = <Item>[];
    final provider = context.watch<ThemeProvider>(); // 监听 Provider 状态变更
    final mode = provider.mode; // 获取当前主题模式
    // final selected = provider.language; // 之前选择的语言

    // 测试信息
    // print("当前语言：$selected");

    // 跟随系统
    items.add(
      Item.switcher(
        title: '跟随系统',
        value: provider.isFollowSystem,
        subtitle: '跟随系统主题',
        onChanged: (value) {
          if (value) {
            provider.saveMode(ThemeMode.system);
            provider.refresh();
          } else {
            // 如果取消跟随系统，则需要手动设置主题
            final isDark = Helper.isDarkMode(context);
            provider.saveMode(isDark ? ThemeMode.dark : ThemeMode.light);
            provider.refresh();
          }
        },
      ),
    );
    if (!provider.isFollowSystem) {
      // 相关性描述标签
      items.add(Item.leading('手动选择'));

      // 循环添加支持的所有语言列表
      items.add(
        Item.select(
          title: '普通模式',
          value: mode == ThemeMode.light,
          onChanged: (value) {
            if (ThemeMode.light != mode) {
              provider.saveMode(ThemeMode.light);
              provider.refresh();
            }
          },
        ),
      );
      items.add(
        Item.select(
          title: '深色模式',
          value: mode == ThemeMode.dark,
          onChanged: (value) {
            if (ThemeMode.dark != mode) {
              provider.saveMode(ThemeMode.dark);
              provider.refresh();
            }
          },
        ),
      );
    }

    // 页面的主题框架
    return Scaffold(
      appBar: AppBar(title: Text('主题设置')),
      body: GroupSetting(children: items), // Flutter | 第16期 - 通用设置页
    );
  }
}
